/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.news;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.services.manage.news.NewsService;
import shop.services.manage.news.bean.News;
import shop.services.manage.system.bean.User;

import javax.servlet.http.HttpServletRequest;

/**   
 * @类名称：NewsAction      
 * @创建人：Ltz   
 * @创建时间：2017-09-20 下午15:47:58      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/news/")
public class NewsAction extends BaseController<News> {
    private static final Logger logger = LoggerFactory.getLogger(NewsAction.class);
    @Autowired
    private NewsService newsService;
    private static final String page_toList = "/manage/news/newsList";
    private static final String page_toAdd = "/manage/news/newsAdd";
    private static final String page_toEdit = "/manage/news/newsEdit";
    public NewsService getService() {
        return newsService;
    }

    private NewsAction(){
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }

    /**
     * 删除单个资讯
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "deleteByIdJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteByIdJson(@RequestParam(value = "id") int id) throws Exception {
        JSONResult jsonResult=new JSONResult();
        getService().deleteById(id);
        jsonResult.setMessage("success");
        return jsonResult;
    }


    /**
     * 新增资讯，为了添加发布者
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertNewsJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertNewsJson(HttpServletRequest request, @ModelAttribute("e") News e) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        e.setCreateUser(user.getNickname());
        getService().insert(e);
        return jsonResult;
    }

    /**
     * 编辑资讯，为了改变发布者
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "updateNewsJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateNewsJson(HttpServletRequest request, @ModelAttribute("e") News e) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        e.setCreateUser(user.getNickname());
        getService().update(e);
        return jsonResult;
    }
}
